package com.atung.parking.export;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.atung.parking.enums.ExportType;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.http.MediaTypeFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.MimeType;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
@Component
public class PublicExportUtil {
    public void exportReport(
            ExportType exportType,
            List exportVOS,
            HttpServletResponse response) throws Exception{
        final HorizontalCellStyleStrategy horizontalCellStyleStrategy = this.publicInitCreateStyle(exportType, response);
        Class a=exportType.getClassName();
        EasyExcel.write(response.getOutputStream(),a )
                .autoCloseStream(Boolean.FALSE)
                .excelType(ExcelTypeEnum.XLSX)
                .registerWriteHandler(horizontalCellStyleStrategy)
                //自动列宽策略
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                .sheet(exportType.getSheetName())
                //获取数据填充
                .doWrite(exportVOS);
    }
    public HorizontalCellStyleStrategy  publicInitCreateStyle(ExportType exportType,
                             HttpServletResponse response) throws Exception{
        String fileNameOri = exportType.getFileName();
        // 文件名中文名需要转义
        String fileName = URLEncoder.encode(fileNameOri, "UTF-8");
        String contentType = MediaTypeFactory.getMediaType(fileName)
                .map(MimeType::toString).orElse("application/vnd.ms-excel");
        response.setContentType(contentType);
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        // 这里需要设置不关闭流
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        //设置背景颜色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 11);
        headWriteCellStyle.setWriteFont(headWriteFont);
        //内容策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        WriteFont contentWriteFont = new WriteFont();
        // 字体大小
        contentWriteFont.setFontHeightInPoints((short) 10);
        contentWriteCellStyle.setWriteFont(contentWriteFont);
//        //设置 水平居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
    }
}
